import Vue from 'vue'
import Router from 'vue-router'
import main from '@/components/main'
import store from '../store/user'
Vue.use(Router)

// 路由配置
const RouterConfig = {
  // mode: 'history',
  routes: [
    {
      path: '/',
      name: 'main',
      redirect: '/count',
      component: main,
      meta: {
        title: '云浮市健康扶贫大数据综合管理平台',
      },
      children: [{
        path: 'count',
        icon: 'md-analytics',
        name: 'count',
        meta: {
          title: '统计汇报',
        },
        component: () =>
          import('@/components/count.vue')
      },
      {
        path: 'manage',
        icon: 'logo-dropbox',
        name: 'manage',
        meta: {
          title: '资金管理',
        },
        component: () =>
          import('@/components/manage.vue')
      },
      {
        path: 'evolve',
        icon: 'md-beaker',
        name: 'evolve',
        meta: {
          title: '工作进展',
        },
        component: () =>
          import('@/components/evolve.vue')
      },
      {
        path: 'application',
        icon: 'md-cube',
        name: 'application',
        meta: {
          title: '智能应用',
        },
        component: () =>
          import('@/components/application.vue')
      },
      ]
    },
    {
      path: '/login',
      name: 'login',
      meta: {
        title: '登录',
      },
      component: () =>
        import('@/components/login.vue')
    },
  ]
};

export const router = new Router(RouterConfig)

router.beforeEach((to, from, next) => {
  let title = to.meta.title;
  let iTitle = router.options.routes[0].meta.title;

  // 设置页面标题
  if (title) {
    iTitle = title + '-' + iTitle;
  }
  window.document.title = iTitle;

  // 检查 Vuex 的 store 中是否有 token
  const token = store.getters.getToken;
  if (to.path !== '/login' && !token) {
    // 如果不是访问登录页且没有 token，则重定向到登录页
    next('/login');
  } else {
    next(); // 如果是访问登录页，则正常进入
  }
})
